<?php

function list_spd($key,$kbulan,$ktahun,$kstatus){

//PASANG JAVASCRIPT
	my_set_code_js('

		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
			}
			return false;
		}

	');	
	$header = array(
		'#'				=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Nomor'			=>array('style'=>'border-bottom:2px solid;width:10%'),   
		'Tgl. Mulai'	=>array('style'=>'text-align:center;border-bottom:2px solid;width:13%'),  
		'Tgl. Selesai'	=>array('style'=>'text-align:center;border-bottom:2px solid;width:13%'),  
		'K/N'			=>array('style'=>'border-bottom:2px solid;width:4%'),  
		'Nama'			=>array('style'=>'text-align:center;border-bottom:2px solid;width:22%'),  
		'Gol'			=>array('style'=>'text-align:center;border-bottom:2px solid;width:3%'),  
		'Keterangan'	=>array('style'=>'text-align:center;border-bottom:2px solid;width:20%'),  
		' '				=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'), 
	);
	$query = "SELECT * FROM spd a WHERE 1 "; 
	if(trim($key)<>''){
		$query .= " AND (a.nomor='{$key}' 
			OR (a.tipe_karyawan='Karyawan' AND a.karyawan_id IN (select b.karyawan_id FROM karyawan b INNER JOIN biodata c ON c.biodata_id = b.biodata_id WHERE c.nama LIKE '%{$key}%' )) 
			OR (a.tipe_karyawan='Non Karyawan' AND a.karyawan_id IN (select d.non_karyawan_id FROM spd_non_karyawan d WHERE d.nama LIKE '%{$key}%' )))";
	}
	if($kstatus==1){
		$query .= " AND a.tipe_karyawan='Karyawan'" ;
	}	
	if($kstatus==2){
		$query .= " AND a.tipe_karyawan='Non Karyawan'" ;
	}	
	if($kbulan<>0 AND $ktahun<>''){
		$query .= " AND ('{$ktahun}-{$kbulan}' BETWEEN left(a.tanggal_mulai,7) AND left(a.tanggal_selesai,7))";
	}	
	$query .= " ORDER BY spd_id  DESC";
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&key={$key}&kbulan={$kbulan}&ktahun={$ktahun}&kstatus={$kstatus}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';	
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$i++;
				
		$editproperty = array(
				'href'=>'index.php?com='.$_GET['com'].'&task=edit&id='.$ey['spd_id'],
				'rel'=>'facebox',
				'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		$detailproperty = array(
				'href'=>'index.php?com=spd&task=detail&id='.$ey['spd_id'].'&key='.$key.'&kbulan='.$kbulan.'&ktahun='.$ktahun.'&kstatus='.$kstatus.'&page='.$_GET['page'],
				'title'=>'Detail'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$deleteproperty = array(
				'href'=>'javascript:; ',
				'onclick'=>'javascript:confirmDelete('.$ey['spd_id'].');',
				'title'=>'Delete'
				);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		if($ey['tipe_karyawan']=='Non Karyawan'){
			$karyawans = my_get_data_by_id('spd_non_karyawan','non_karyawan_id',$ey['karyawan_id'] );
			$golongan_jabatan_id = isset( $karyawans['golongan_jabatan_id'] ) ? $karyawans['golongan_jabatan_id'] :  0; 
			$golongans = my_get_data_by_id('golongan_jabatan','golongan_jabatan_id',$golongan_jabatan_id );
			$row[] = array(
				'#'=>position_text_align ($i, 'center'), 
				'nomor'=>  strtoupper($ey['nomor'] ),   
				'start'=>  position_text_align(  date('d - m - Y' ,strtotime($ey['tanggal_mulai']  )),   'center' ),
				'end'=> position_text_align(   date('d - m - Y' ,strtotime($ey['tanggal_selesai']  )),  'center' ), 
				'nk'=>'N',
				'nama'=>$karyawans['nama'],
				'gol'=>$golongans['kode_golongan_jabatan'],
				'ket'=>  $ey['keterangan'] ,   
				'operasi'=> position_text_align(  $edit_button .' '.$detail_button.' '.$delete_button , 'right'),  
			);			

		}else{
			$karyawans = loaddata_karyawan($ey['karyawan_id']) ;
			$row[] = array(
				'#'=>position_text_align ($i, 'center'), 
				'nomor'=>  strtoupper($ey['nomor'] ),   
				'start'=>  position_text_align(  date('d - m - Y' ,strtotime($ey['tanggal_mulai']  )),   'center' ),
				'end'=> position_text_align(   date('d - m - Y' ,strtotime($ey['tanggal_selesai']  )),  'center' ), 
				'nk'=>'K',
				'nama'=>$karyawans['nama_gelar'],
				'gol'=>$karyawans['golongan_jabatan'],
				'ket'=>  $ey['keterangan'] ,   
				'operasi'=> position_text_align(  $edit_button .' '.$detail_button.' '.$delete_button , 'right'),  
			);
		}		
	}
	
	$datas = table_rows($row); 
	if(is_admin()):
		$menuontop = array(  
			'Tambah Data SPD'=>array( 'style'=>"width:180px;", 'onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com=spd&task=&task=edit&id=0\' });'),			
			'Tambah Data Non Karyawan'=>array( 'style'=>"width:270px;", 'onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com=spd_non_karyawan&task=edit&id=0\' });'),
		);	
	endif;	
	$bulans  = array(
		'01'=>'01', 
		'02'=>'02', 
		'03'=>'03', 
		'04'=>'04', 
		'05'=>'05', 
		'06'=>'06', 
		'07'=>'07', 
		'08'=>'08', 
		'09'=>'09', 
		'10'=>'10', 
		'11'=>'11', 
		'12'=>'12', 
	);
	$bulan = array(
		'name'=>'kbulan',
		'value'=>isset($_GET['kbulan']) ? $_GET['kbulan']: ''  
	);
	$bulan_form = form_dropdown( $bulan,$bulans);
	
	$tahun = array(
		'name'=>'ktahun','size'=>5,
		'value'=>isset($_GET['ktahun']) ? $_GET['ktahun']: ''
	); 
	$tahun_form = form_dynamic($tahun );

	$temp=array();
	$temp[1]="Karyawan" ;
	$temp[2]="Non Karyawan" ;
	$form_status = array(
		'name'=>'kstatus',
		'id'=>'kstatus',
		'value'=>isset($_GET['kstatus']) ? $_GET['kstatus'] : 0 
	);	
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" />
	<span style="font:10px verdana"><b>Cari Nomor / Nama</b></span>
	<input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" />
	<span style="font:10px verdana"><b>Periode</b></span>'.$bulan_form.$tahun_form.'
	<span style="font:10px verdana"><b>Status</b></span>'.form_dropdown($form_status , $temp).'
	<input class="simple_search" type="submit" value=" CARI " /></form>';
	$printmenu = additional_menu_on_list( $menuontop, false);
	$paging = $kgPagerOBJ ->showPaging();
	return $search.$printmenu.table_builder($header , $datas ,  5 , false,$paging );	
	return $search.$printmenu .table_builder($header , $datas ,  5 , false,$paging );	
}

function form_spd($id =0,$valid_post,$validsubmit){
	my_set_file_js(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.js' ,
			'components/system/js/calendar/calendarDateInput.js'
		)
	);
	my_set_file_css(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.css' 
		)
	);

	my_set_code_js('  
		function findValue(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

		function selectItem(li) {
			findValue(li);
		}

		function formatItem(row) {
			return   row[0] ;
		}

		function lookupAjax(){
			var oSuggest = $("#nama_karyawan")[0].autocompleter;
			oSuggest.findValue(); 
			return false;
		}
		$(document).ready(function() {
			$("#nama_karyawan").autocomplete(
				"autocomplete_daftar_nama_spd.php",
				{
					delay:10,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:5,
					onItemSelect:selectItem,
					onFindValue:findValue,
					formatItem:formatItem,
				//	autoFill:true
				}
			);
		});
	
		function checkName(name){
			$.get("check_karyawan_names.php", { nama: name  },
			function(data){
				if(data ==  \'0\' ){
					alert(\'Nama karyawan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\');
					$(\'#nama_karyawan\').val(\'\');
					return false;
			} 
			});
		}
	');
	$fields = my_get_data_by_id('spd','spd_id',$id);
	$view = form_header( "edit data" , "edit data"  );
	$view .= form_field_display( ($fields ? $fields['nomor']:'STPD'.date('Y').get_nomor_baru() )   , "Nomor STPD");
	if( $valid_post=='' ){
		$valid_post='submit1';
	}
	$tipe_karyawan=(isset($_POST['tipe_karyawan'])? $_POST['tipe_karyawan'] : ( $fields ? $fields['tipe_karyawan'] : '') );	
	if(	$valid_post=='submit1' AND !$validsubmit){	
		$atemps=array();
		$atemps["Karyawan"]="Karyawan" ;
		$atemps["Non Karyawan"]="Non Karyawan" ;
		$temp = array(
			'name'=>'tipe_karyawan',
			'value'=>$tipe_karyawan,
			'id'=>'tipe_karyawan' 
		);
		$form_temp = form_dropdown($temp , $atemps );
		$submit = array(
			'value' => ( '  Submit  '),
			'name' => 'submit1', 
			'type'=>'submit'
		);
		$form_submit= form_dynamic($submit); 
		$view .= form_field_display( $form_temp .' '.$form_submit  ,"Tipe karyawan"  );	
	}else{
		$view .= form_field_display( get_form_hidden('tipe_karyawan',$_POST['tipe_karyawan']).$tipe_karyawan ,"Tipe karyawan"  );
		if(	$valid_post=='submit1' AND $validsubmit){
			$valid_post='submit2' ; $validsubmit=false;
		}		
	}

	if(  isset($_POST['nama_karyawan'] ) ){
		$fields_nama=$_POST['nama_karyawan'] ;	
		$karyawan_id = get_karyawan_id_or_nama_id($fields_nama,$tipe_karyawan);				
	}elseif($fields){
		$karyawan_id = $fields['karyawan_id'] ;
		if($tipe_karyawan=='Karyawan'){
			$field_temp = loaddata_karyawan($karyawan_id);
			$fields_nama= $field_temp['nik'].'/'.$field_temp['nama'] ;
		}else{
			$field_temp = my_get_data_by_id('spd_non_karyawan', 'non_karyawan_id' ,$karyawan_id);
			$fields_nama= $field_temp['nama'] ;
		}
	}else{
		$karyawan_id = 0;
		$fields_nama= "" ;	
	}	
	if( $tipe_karyawan=="Karyawan" ){	
		$profesi=get_profesi($karyawan_id);
		$jenis_non_karyawan_id=0;
	}else{	
		$profesi='';
		$jenis_non_karyawan_id=get_jenis_karyawan($karyawan_id);
	}			
	if(	$valid_post=='submit2' AND !$validsubmit){
		$temp = array(
			'name'=>'nama_karyawan',
			'value'=>( $fields_nama ),
			'id'=>'nama_karyawan' , 
			'type'=>'text',
			'size'=>'45' , 
		);		$form_temp = form_dynamic($temp);
		$submit = array(
			'value' => ( '  Submit  '),
			'name' => 'submit2', 
			'type'=>'submit'
		);
		$form_submit= form_dynamic($submit); 		
		$view .= form_field_display( $form_temp.' '.$form_submit  , ($tipe_karyawan=="Karyawan" ? "NIK/Nama Karyawan" : "Nama Non Karyawan") );
	}else{
		$view .= form_field_display( get_form_hidden('nama_karyawan',$_POST['nama_karyawan']).$fields_nama ,($tipe_karyawan=="Karyawan" ? "NIK/Nama Karyawan" : "Nama Non Karyawan") );

		if(	$valid_post=='submit2' AND $validsubmit){
			$valid_post='submit3' ; $validsubmit=false;
		}
	}
	$tanggal_mulai=(isset($_POST['tanggal_mulai'])? $_POST['tanggal_mulai'] : ( $fields ? date('d-m-Y' , strtotime($fields['tanggal_mulai'])) : date('d-m-Y')) );
	$tanggal_selesai=(isset($_POST['tanggal_selesai'])? $_POST['tanggal_selesai'] : ( $fields ? date('d-m-Y' , strtotime($fields['tanggal_selesai'])) : date('d-m-Y')) );		
	$keperluan_id=(isset($_POST['keperluan_id'])? $_POST['keperluan_id'] : ( $fields ? $fields['keperluan_id'] : 0 ) );	
	$keperluans=my_get_data_by_id('spd_keperluan','keperluan_id',$keperluan_id);
	$keperluan=(isset($_POST['keperluan'])? $_POST['keperluan'] : ( $fields ? $fields['keperluan'] : '' ) );	

	if(	$valid_post=='submit3' AND !$validsubmit){
		$temp = array(
				'name'=>'tanggal_mulai',
				'value'=>$tanggal_mulai,
				'id'=>'tanggal_mulai', 
			);
		$form_temp = form_calendar($temp);
		$view .= form_field_display( $form_temp  , "Tanggal mulai"  );
		$temp = array(
				'name'=>'tanggal_selesai',
				'value'=>$tanggal_selesai,
				'id'=>'tanggal_selesai', 
			);
		$form_temp = form_calendar($temp);
		$view .= form_field_display( $form_temp , "Tanggal selesai"  );
		$atemps = array();
		if( $tipe_karyawan=="Karyawan" ){
			$query = " SELECT * FROM spd_keperluan" ;
			if( trim( $profesi )=='' ){
				$query .= " WHERE lebih_1_hari='T'";
			}	
			$query .= " ORDER BY label_keperluan";
			$result = my_query($query);
			while( $u = my_fetch_array($result)){
				$atemps[$u['keperluan_id']] = $u['label_keperluan'];
			}
			$temp = array(
					'name'=>'keperluan_id',
					'value'=>$keperluan_id,
					'id'=>'keperluan_id', 
				);
			$form_temp = form_dropdown($temp , $atemps);
			$form_hidden=get_form_hidden('keperluan',' ') ; 
		}else{	
			$query = "SELECT a.keperluan_id, b.label_keperluan FROM spd_jenis_non_karyawan_keperluan a 
				INNER JOIN spd_keperluan b ON a.keperluan_id=b.keperluan_id 
				WHERE jenis_non_karyawan_id={$jenis_non_karyawan_id} 
				ORDER BY b.label_keperluan" ;
			$result = my_query($query);
			if( my_num_rows($result)>0 ){
				while( $u = my_fetch_array($result)){
					$atemps[$u['keperluan_id']] = $u['label_keperluan'];
				}
				$temp = array(
					'name'=>'keperluan_id',
					'value'=>$keperluan_id,
					'id'=>'keperluan_id', 
				);
				$form_temp = form_dropdown($temp , $atemps);
				$form_hidden=get_form_hidden('keperluan',' ') ; 				
			}else{
				$temp = array(
					'name'=>'keperluan',
					'value'=>$keperluan,
					'id'=>'keperluan',
					'type'=>'textfield',
					'size'=>'30'
				);
				$form_temp = form_dynamic($temp);
				$form_hidden=get_form_hidden('keperluan_id',4) ; //4=keperluan-> Lain-lain
			}	
		}
		$submit = array(
			'value' => ( '  Submit  '),
			'name' => 'submit3', 
			'type'=>'submit'
		);
		$form_submit= form_dynamic($submit); 		
		$view .= form_field_display( $form_hidden.$form_temp.' '.$form_submit , "Keperluan"  );	
	}else{
		$view .= form_field_display( get_form_hidden('tanggal_mulai',$_POST['tanggal_mulai']).date('d-m-Y' , strtotime($tanggal_mulai))   , "Tanggal mulai"  );
		$view .= form_field_display( get_form_hidden('tanggal_selesai',$_POST['tanggal_selesai']).date('d-m-Y' , strtotime($tanggal_selesai))   , "Tanggal selesai"  );
		if( $tipe_karyawan=="Karyawan" OR get_ada_keperluan_non_karyawan($jenis_non_karyawan_id)){		
			$view .= form_field_display( get_form_hidden('keperluan',$_POST['keperluan']).get_form_hidden('keperluan_id',$_POST['keperluan_id']).$keperluans['label_keperluan']   , "Keperluan"  );		
		}else{
			$view .= form_field_display( get_form_hidden('keperluan',$_POST['keperluan']).get_form_hidden('keperluan_id',$_POST['keperluan_id']).$keperluan   , "Keperluan"  );		
		}
		if(	$valid_post=='submit3' AND $validsubmit){
			$valid_post='submit' ; $validsubmit=false;
		}	
	}
	$atemps = list_kalender(form_date_converter($tanggal_mulai) , form_date_converter($tanggal_selesai));
	$jumlah_hari = count($atemps);		
	$jam_p3k=(isset($_POST['jam_p3k'])? $_POST['jam_p3k'] : ( $fields ? $fields['jam_p3k'] : '0' ) );	
	$kendaraan_id=(isset($_POST['kendaraan_id'])? $_POST['kendaraan_id'] : ( $fields ? $fields['kendaraan_id'] : 0 ) );	
	$nomor_polisi=(isset($_POST['nomor_polisi'])? $_POST['nomor_polisi'] : ( $fields ? $fields['nomor_polisi'] : ' ' ) );	
	$kota=(isset($_POST['kota'])? $_POST['kota'] : ( $fields ? $fields['kota'] : '' ) );	
	$keterangan=(isset($_POST['keterangan'])? $_POST['keterangan'] : ( $fields ? $fields['keterangan'] : '' ) );

	if(	$valid_post=='submit' AND !$validsubmit){
		if($keperluans['p3k']=='Y' AND $tanggal_mulai==	$tanggal_selesai){
			$temp = array(
				'name'=>'jam_p3k',
				'value'=>$jam_p3k,
				'id'=>'nomor_polisi',
				'type'=>'textfield',
				'size'=>'15'
			);	
			$form_temp = form_dynamic($temp , $atemps);
			$view .= form_field_display( $form_temp , "Jam P3K"  );		
		}else{
			$view .= form_field_display( get_form_hidden('jam_p3k',$_POST['jam_p3k']).$jumlah_hari.' hari' , "Lama"  );		
		}


		$atemps = array();
		if( $tipe_karyawan=="Karyawan" ){		
			$query = " SELECT * FROM spd_kendaraan ORDER BY label_kendaraan";
		}else{
			$query = " SELECT * FROM spd_jenis_non_karyawan_kendaraan a 
			INNER JOIN spd_kendaraan b ON a.kendaraan_id=b.kendaraan_id 
			WHERE a.jenis_non_karyawan_id={$jenis_non_karyawan_id} 
			ORDER BY b.label_kendaraan";
		}
		$result = my_query($query);
		while( $u = my_fetch_array($result)){
			$atemps[$u['kendaraan_id']] = $u['label_kendaraan'];
		}
		$temp = array(
				'name'=>'kendaraan_id',
				'value'=>$kendaraan_id,
				'id'=>'kendaraan_id', 
			);
		$form_temp = form_dropdown($temp , $atemps);
		$view .= form_field_display( $form_temp , "Kendaraan"  );
			
		$temp = array(
				'name'=>'nomor_polisi',
				'value'=>$nomor_polisi,
				'id'=>'nomor_polisi',
				'type'=>'textfield',
				'size'=>'15'
			);
		$form_temp = form_dynamic($temp);
		$view .= form_field_display( $form_temp  , "Nomor polisi"  );
		$temp = array(
				'name'=>'kota',
				'value'=>$kota,
				'id'=>'kota',
				'type'=>'textfield',
				'size'=>'35'
			);
		$form_temp = form_dynamic($temp);
		$view .= form_field_display( $form_temp  , "Kota tujuan"  );
		$temp = array(
				'name'=>'keterangan',
				'value'=>$keterangan,
				'id'=>'keterangan', 
			);
		$form_temp = form_textarea($temp  );
		$view .= form_field_display( $form_temp , "Keterangan"  );
		$submit = array(
			'value' => ( $id ==0 ? ' Simpan ' :'  Simpan  '),
			'name' => 'submit', 
			'type'=>'submit','class'=>'main_button'
		);
		$form_submit= form_dynamic($submit); 
		$view .= form_field_display( $form_submit, "&nbsp;" ,  "" );
	}else{
		if($keperluans['p3k']=='Y' AND $tanggal_mulai==	$tanggal_selesai){
			$view .= form_field_display( get_form_hidden('jam_p3k',$_POST['jam_p3k']).$jam_p3k , "Jam P3K"  );		
		}else{
			$view .= form_field_display( get_form_hidden('jam_p3k',$_POST['jam_p3k']).$jumlah_hari.' hari' , "Lama"  );		
		}	
		$kendaraans=my_get_data_by_id('spd_kendaraan','kendaraan_id',$kendaraan_id);
		$view .= form_field_display( get_form_hidden('kendaraan_id',$_POST['kendaraan_id']).$kendaraans['label_kendaraan'] , "Kendaraan"  );
		$view .= form_field_display( get_form_hidden('nomor_polisi',$_POST['nomor_polisi']).$nomor_polisi , "Nomor polisi"  );
		$view .= form_field_display( get_form_hidden('kota',$_POST['kota']).$kota , "Kota tujuan"  );
		$view .= form_field_display( get_form_hidden('keterangan',$_POST['keterangan']).$keterangan  , "Keterangan"  );
	}
	$view .= form_footer( );
	return $view;
}

function spd_validate($id){
	$errsubmit = false;
	$err = array();
	if($_POST['tipe_karyawan']=="0"){
		$errsubmit = true;
		$err[] ="Tipe Karyawan tidak boleh kosong";
	}
	if(isset($_POST['nama_karyawan'])){
		$karyawan_id = get_karyawan_id_or_nama_id($_POST['nama_karyawan'],$_POST['tipe_karyawan']);	
		if( $karyawan_id == 0){
			$errsubmit = true;
			$err[] ="NIK/Nama Karyawan salah";
		}
	}
	if( $errsubmit){
		return $err;
	}	
	return $errsubmit;
}

function submit_spd($id = 0){
	$karyawan_id=get_karyawan_id_or_nama_id($_POST['nama_karyawan'],$_POST['tipe_karyawan']) ;
	$datas = array(
		'nomor'	=>	my_type_data_str( 'STPD'.date('Y').get_nomor_baru() 	),
		'kota'=>my_type_data_str($_POST['kota']),
		'tanggal_mulai'=>my_type_data_str(form_date_converter($_POST['tanggal_mulai']) ),
		'tanggal_selesai'=>my_type_data_str(form_date_converter($_POST['tanggal_selesai'])),
		'keperluan_id'=>my_type_data_int($_POST['keperluan_id']),
		'keperluan'=>my_type_data_str($_POST['keperluan']),
		'keterangan'=>my_type_data_str($_POST['keterangan']),
		'kendaraan_id'=>my_type_data_int($_POST['kendaraan_id']),
		'nomor_polisi'=>my_type_data_str($_POST['nomor_polisi']),
		'karyawan_id'=>my_type_data_int($karyawan_id),
		'tipe_karyawan'=>my_type_data_str($_POST['tipe_karyawan']),
		'keterangan'=>my_type_data_str($_POST['keterangan']),
		'jam_p3k'=>my_type_data_str( (int) $_POST['jam_p3k']),
	);
		
	if( $id > 0 ){
		my_update_record('spd' ,  'spd_id', $id , $datas);	
	}else{
		$id = my_insert_record( 'spd' , $datas);
	}	
	return stor_spd_detail_data($id);
}

function remove_spd($id){
	$query ="DELETE FROM spd  WHERE  spd_id ={$id}";
	my_query($query);	
	$query = "DELETE from spd_detail WHERE spd_id ={$id} ";
	return my_query($query);
}


// ============================= SPD DETAIL ==================================== //

function list_detail($sid){

	$header = array(
		'#'				=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Jenis Biaya'	=>array('style'=>'border-bottom:2px solid;width:25%'),   
		'Tarif (Rp)'	=>array('style'=>'text-align:right;border-bottom:2px solid;width:20%'),  
		'Jumlah'		=>array('style'=>'text-align:right;border-bottom:2px solid;width:20%'),  
		'Total (Rp)'	=>array('style'=>'text-align:right;border-bottom:2px solid;width:20%'),  
		 	' '			=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'), 
	);	
	$query = "SELECT * FROM spd_detail  a
		INNER JOIN spd_biaya b ON a.biaya_id = b.biaya_id
		WHERE a.spd_id ={$sid} 
		ORDER BY b.kode_biaya";
	$result = my_query($query);
	$row = array();
	$i=0;
	while( $ey = my_fetch_array($result) ){
		$i++; 
		$editproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=edit_detail&sid='.$sid.'&id='.$ey['spd_detail_id'],
			'rel'=>'facebox',
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'jenis'=>  $ey['label_biaya'] ,   
			'tarif'=> position_text_align( rp_format($ey['tarif']) ,  'right'), 
			'jumlah'=> position_text_align( $ey['jumlah'].' hari' ,  'right'), 
			'total'=>position_text_align( rp_format($ey['total']) ,  'right'), 
			'operasi'=> position_text_align(  $edit_button  , 'right'),  	
		);
	}	
	$datas = table_rows($row);
	$view = form_header( "devisi" , "devisi"  );
	
 	$fields = my_get_data_by_id('spd','spd_id',$sid);		
	$keperluan = my_get_data_by_id('spd_keperluan','keperluan_id',isset($fields['keperluan_id']) ? $fields['keperluan_id'] : 0);
	$kendaraan = my_get_data_by_id('spd_kendaraan','kendaraan_id',isset($fields['kendaraan_id']) ? $fields['kendaraan_id'] : 0);
	if($fields['tipe_karyawan']=='Non Karyawan'){
		$karyawans = my_get_data_by_id('spd_non_karyawan','non_karyawan_id',$fields['karyawan_id'] );
		$golongan_jabatan_id = isset( $karyawans['golongan_jabatan_id'] ) ? $karyawans['golongan_jabatan_id'] :  0; 
		$golongans = my_get_data_by_id('golongan_jabatan','golongan_jabatan_id',$golongan_jabatan_id );
		$view .= form_field_display( $fields['nomor'].'&nbsp; &nbsp;  &nbsp; &nbsp;  <b>Nama :</b>  '.$karyawans['nama'].
			'&nbsp; &nbsp;  &nbsp; &nbsp;  <b>Golongan :</b>  '.$golongans['kode_golongan_jabatan'], "Nomor");
	}else{
		$karyawans = loaddata_karyawan($fields['karyawan_id']) ;
		$view .= form_field_display( $fields['nomor'].
			'&nbsp; &nbsp;  &nbsp; &nbsp;  <b>NIK :</b>  '.$karyawans['nik'].
			'&nbsp; &nbsp;  &nbsp; &nbsp;  <b>Nama :</b>  '.$karyawans['nama_gelar'].
			'&nbsp; &nbsp;  &nbsp; &nbsp;  <b>Golongan :</b>  '.$karyawans['golongan_jabatan'], "Nomor");
		}		
	$view .= form_field_display( date('d - m - Y' , strtotime($fields['tanggal_mulai'] )).' s/d '.   
		date('d - m - Y' , strtotime($fields['tanggal_selesai'] )), "Tanggal"   );
	if(  (int) $fields['jam_p3k']==0 ){
	}else{
		$view .= form_field_display( $fields['jam_p3k'].' jam'  , "P3K"   );
	}	
		
	$view .= form_field_display( $fields['kota']   , " Kota"   );
	$view .= form_field_display( $keperluan['label_keperluan']   , " Keperluan"   );
	$view .= form_field_display( $kendaraan['label_kendaraan']   , " Kendaraan"   );
	if(trim($fields['nomor_polisi']) <> '')
		$view .= form_field_display( $fields['nomor_polisi']   , " Nomor Polisi"   );	
	$view .= form_footer( );
	$menuontop = array(  
		'Kembali' => array('onclick'=>'javascript:location.href=\'index.php?com=spd&key='.$key.'&kbulan='.$kbulan.'&ktahun='.$ktahun.'&kstatus='.$kstatus.'&page='.$_GET['page'].'\''), 
		'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com='.$_GET['com'].'&task=plaincetak&id='.$sid.'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');'),
//		'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com='.$_GET['com'].'&task=plaincetak&key='.$key.'&kbulan='.$kbulan.'&ktahun='.$ktahun.'&kstatus='.$kstatus.'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');'),
	);
	$printmenu = additional_menu_on_list( $menuontop , '');
	return $view.$printmenu.table_builder($header , $datas , 7 , false );
}

function form_spd_detail($sid,$id=0){
	$view = form_header( "devisi" , "devisi"  );
//	$spds = my_get_data_by_id('spd','spd_id',$sid);
 	$fields = my_get_data_by_id('spd_detail','spd_detail_id',$id);
	$biayas = my_get_data_by_id('spd_biaya','biaya_id',$fields['biaya_id']);
	$view .= form_field_display( $biayas['label_biaya']   , "Jenis biaya"   );
	
	if(  $biayas['tabel_transport']=="Y" ){
		$view .= spd_transport_list() ;
	}	
	if( (int) $fields['tarif'] ==0){ 
		$view .= form_field_display( $fields['jumlah'].' hari'  , "Jumlah"  );		
		$temp = array(
			'name'=>'total',
			'value'=>( isset($_POST['total']) ? $_POST['total'] : rp_format($fields['total']) ),
			'id'=>'total',
			'type'=>'text',
			'size'=>'25'
		);
		$form_temp = form_dynamic($temp);
		$view .= form_field_display( $form_temp  , "Total" );	
	}else{
		$view .= form_field_display( rp_format($fields['tarif'])   , "Tarif"   );
		$temp = array(
			'name'=>'jumlah',
			'value'=>( isset($_POST['jumlah']) ? $_POST['jumlah'] : $fields['jumlah'] ),
			'id'=>'jumlah',
			'type'=>'text',
			'size'=>'10'
		);
		$form_temp = form_dynamic($temp);
		$view .= form_field_display( $form_temp.' hari'  , "Jumlah" );
		$view .= form_field_display( rp_format($fields['total']) , "Total" );					
	 }
	$datas=get_total_link($id, 0 );
	if( $datas ){
		$view .= form_field_display(rp_format( $datas['total'])   , "Biaya lainnya"   );
		$view .= form_field_display( rp_format($datas['maksimal'])   , "Maksimal"   );
	}		
	 
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 

	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function spd_detail_validate($sid,$id){
	$errsubmit = false;
	$err = array();

	$fields = my_get_data_by_id('spd_detail','spd_detail_id',$id);
	if(isset($_POST['jumlah'])){
		$jumlah = $_POST['jumlah'];
		$total = $_POST['jumlah'] *$fields['tarif'];
	}else{
		$jumlah = $fields['jumlah'] ;
		$total = set_text_as_number($_POST['total']);
	}	
	$datas=get_total_link($id, $total);
	if( $datas ){
		if($datas['total'] > $datas['maksimal'] ){
			$errsubmit = true;
			$err[] = "Total Biaya tidak boleh melebihi maksimal";		
		}
	}	
	if( $errsubmit){
		return $err;
	}	
	return $errsubmit;
}

function submit_spd_detail($sid,$id = 0){
	$fields = my_get_data_by_id('spd_detail','spd_detail_id',$id);
	if(isset($_POST['jumlah'])){
		$jumlah = $_POST['jumlah'];
		$total = $_POST['jumlah'] *$fields['tarif'];
	}else{
		$jumlah = $fields['jumlah'] ;
		$total = set_text_as_number($_POST['total']);
	}
	
	$datas = array(
		'jumlah'=>my_type_data_str($jumlah),
		'total'=>my_type_data_str($total),
	);
	 
	return my_update_record('spd_detail','spd_detail_id',$id,$datas);
	
}

// ============================= UDF ==================================== //

function stor_spd_detail_data($id){
	$spds=my_get_data_by_id('spd','spd_id',$id) ;
	$rdates = list_kalender($spds['tanggal_mulai'] , $spds['tanggal_selesai']  );
	$jumlah_hari = count($rdates);	
	$golongan_jabatan_id =get_golongan_jabatan_id($spds['tipe_karyawan'], $spds['karyawan_id']);		
	$spd_keperluans=my_get_data_by_id('spd_keperluan','keperluan_id',$spds['keperluan_id']) ;
	$tipe_tarif='tarif_spd';
	if( $spds['tipe_karyawan']=="Non Karyawan" ){ //saring dulu u/ non karyawan karena punya tarif khusus (supir)
		$jenis_non_karyawan_id=get_jenis_karyawan($spds['karyawan_id']);
		if( get_ada_tarif_khusus_non_karyawan($jenis_non_karyawan_id) ){
			$tipe_tarif='tarif_khusus_non_karyawan';
		}
	}else{
		$profesi=get_profesi($spds['karyawan_id']);
		if( $spd_keperluans['lebih_1_hari']=="Y" ){
			if($jumlah_hari==1 AND  (int) $spds['jam_p3k'] <= 8){
				$tipe_tarif='tarif_jabatan';
			}	
		}	
	}	
	if( $tipe_tarif=='tarif_khusus_non_karyawan' ){
		$query= "SELECT biaya_id,nominal FROM spd_jenis_non_karyawan_biaya 
			WHERE jenis_non_karyawan_id={$jenis_non_karyawan_id} 
			AND keperluan_id={$spds['keperluan_id']}
			ORDER BY biaya_id";
	}
	if( $tipe_tarif=='tarif_jabatan' ){
		$query= "SELECT biaya_id,".( $profesi=='Dokter' ? "nominal_dokter" : "nominal_perawat")." as nominal FROM spd_biaya_jabatan 
				WHERE keperluan_id={$spds['keperluan_id']} 
				AND (batas_jam < {$spds['jam_p3k']} OR batas_jam='0') 
				ORDER BY biaya_id";
	}
	if( $tipe_tarif=='tarif_spd' ){
		$query= "SELECT a.biaya_id,b.nominal FROM spd_biaya a
			LEFT JOIN spd_biaya_golongan_jabatan b ON b.biaya_id=a.biaya_id AND b.golongan_jabatan_id={$golongan_jabatan_id} 
			WHERE a.biaya_id IN (SELECT biaya_id FROM spd_keperluan_biaya where keperluan_id= {$spds['keperluan_id']} ) 
			AND a.biaya_id IN (SELECT biaya_id FROM spd_kendaraan_biaya where kendaraan_id= {$spds['kendaraan_id']} ) 
			ORDER BY a.biaya_id";
	}
	$result = my_query($query);
	$row = array();
	$biaya_ids='(';
	$i=0;
	while( $ey = my_fetch_array($result) ){
		$biaya_ids.=$ey['biaya_id'].',' ;
		$i++; 
		$biayas=my_get_data_by_id('spd_biaya','biaya_id', $ey['biaya_id']);	
		if( $biayas['tipe']=="Nominal"){
			$nominal=$ey['nominal'] ;
			$maksimal=0;
			$link_biaya_id=0 ;
		}else{
			$nominal=0 ;
			$maksimal=$ey['nominal'] ;
			$link_biaya_id=$biayas['link_biaya_id'] ;
		}
		$jumlah=$jumlah_hari+$biayas['penyesuaian_hari'] ;		
		$query= "SELECT * FROM spd_detail WHERE spd_id = {$id} AND biaya_id = {$ey['biaya_id']}" ;
		$res = my_query($query);
		$ey1 = my_fetch_array($res );		
		if($ey1){
			if( (int) $nominal==0){
				$total=$ey1['total'];
			}else{
				$jumlah=$ey1['jumlah'];
				$total=$nominal * $jumlah ;
			}	
		}else{
			$total=$nominal * $jumlah ;
		}	
		$datas = array(
			'spd_id'=>my_type_data_int($id),
			'biaya_id'=>my_type_data_int($ey['biaya_id']),
			'tarif'=>my_type_data_str($nominal),
			'jumlah'=>my_type_data_str($jumlah),
			'total'=>my_type_data_str($total),
			'maksimal'=>my_type_data_str($maksimal),
			'link_biaya_id'=>my_type_data_int($link_biaya_id),
		);	
		if($ey1){
			my_update_record('spd_detail','spd_detail_id',$ey1['spd_detail_id'],$datas);
		}else{
			my_insert_record('spd_detail' , $datas);	
		}			
	}
	$biaya_ids=substr($biaya_ids, 0, -1).')';  
	$query = "DELETE FROM spd_detail WHERE spd_id = {$id} AND biaya_id NOT IN {$biaya_ids}";
	my_query($query);
}

 function get_golongan_jabatan_id($tipe_karyawan, $karyawan_id){
	if( $tipe_karyawan == 'Karyawan'){
		$query = "SELECT golongan_jabatan_id FROM kekaryawanan WHERE karyawan_id = {$karyawan_id}";
	}else{
		$query = "SELECT golongan_jabatan_id FROM spd_non_karyawan WHERE non_karyawan_id = {$karyawan_id}";
	}
	$result = my_query($query);
	$rw = my_fetch_array($result);
	if( $rw ){
		return $rw['golongan_jabatan_id'] ;
	}	
	return 0 ;
}

function get_profesi($karyawan_id){
	$res = ' ';
	$query = "SELECT profesi_id FROM kekaryawanan WHERE karyawan_id = {$karyawan_id}";
	$rw = my_fetch_array(my_query($query));
	if( $rw ){
		$temps=my_get_data_by_id('profesi','profesi_id', $rw['profesi_id']) ;
		$res = $temps['jabatan'];
	}			
	return $res ;
}

function get_jenis_karyawan($karyawan_id){
	$res = 0;
	$query = "SELECT jenis_non_karyawan_id FROM spd_non_karyawan WHERE non_karyawan_id = {$karyawan_id}";
	$rw = my_fetch_array(my_query($query));
	if( $rw ){
		$res = $rw['jenis_non_karyawan_id'] ;
	}
	return $res ;
}

function get_karyawan_id_or_nama_id($nama,$tipe_karyawan){
	$nama = trim($nama);
	
	if($tipe_karyawan=='Karyawan'){
		list($nik,$temp) = explode("/" ,$nama );
		$nik = rtrim($nik,")");
		$nik = trim($nik);	
		$query = "SELECT karyawan_id FROM karyawan WHERE nik ='{$nik}'";
		$result = my_query($query);
		$row =my_fetch_array($result);
		return $row['karyawan_id'];
	}
	$query = "SELECT non_karyawan_id FROM spd_non_karyawan WHERE nama = '{$nama}'";
	$result = my_query($query);
	$row =my_fetch_array($result);
	return $row['non_karyawan_id'];
}


function get_nomor_baru(){
	$query = "SELECT MAX(spd_id) AS mmax FROM spd WHERE YEAR(tanggal_mulai) = YEAR(NOW())";
	$result = my_query($query);
	$row = my_fetch_array($result);
	return sprintf('%05d' , $row['mmax'] +1);
}

function spd_transport_list(){
	$query = "SELECT * FROM spd_jarak_tempuh order by km ";
	$result = my_query($query);
	$r ='';
	while($row = my_fetch_array($result)){
		$tarifs = my_fetch_array(my_query("SELECT km from spd_jarak_tempuh WHERE km> {$row['km']} ORDER BY km LIMIT 1")) ;
		if( $tarifs['km']>0){
			$judul = '> '.rp_format($row['km']).' - '. rp_format($tarifs['km']).' km' ;
		}else{
			$judul = ' > '.rp_format($row['km']).' km' ;	
		}	
		$r.=form_field_display( rp_format($row['tarif'])   , $judul  );	
	} 
	return $r ;
}


function get_ada_keperluan_non_karyawan($jenis_non_karyawan_id){
	$query = "SELECT keperluan_id FROM spd_jenis_non_karyawan_keperluan WHERE jenis_non_karyawan_id={$jenis_non_karyawan_id}" ;
	$result = my_query($query);
	if( my_num_rows($result)>0 ){
		return true;
	}
	return false;
}	

function get_ada_tarif_khusus_non_karyawan($jenis_non_karyawan_id){
	$query = "SELECT biaya_id FROM spd_jenis_non_karyawan_biaya WHERE jenis_non_karyawan_id={$jenis_non_karyawan_id}" ;
	$result = my_query($query);
	if( my_num_rows($result)>0 ){
		return true;
	}
	return false;
}	

function get_total_link($spd_detail_id, $total){
	$fields = my_get_data_by_id('spd_detail','spd_detail_id',$spd_detail_id);
	$datas=array();
	if( (int) $fields['maksimal']>0){
		if($fields['link_biaya_id']>0){
			$result = my_query("SELECT total FROM spd_detail 
				WHERE spd_id = {$fields['spd_id']} 
				AND biaya_id = {$fields['link_biaya_id']} LIMIT 1") ;
			$ey = my_fetch_array($result);
			$total+=$ey['total'];
		}
		$datas['total']=$total ;
		$datas['maksimal']=$fields['maksimal']*$fields['jumlah'] ;
	}else{
		$result = my_query("SELECT maksimal,total,jumlah FROM spd_detail 
			WHERE spd_id = {$fields['spd_id']} 
			AND link_biaya_id = {$fields['biaya_id']} LIMIT 1") ;
		$ey = my_fetch_array($result);
		if( $ey ){
			$total+=$ey['total'];
			$datas['total']=$total ;
			$datas['maksimal']=$ey['maksimal']*$ey['jumlah'] ;
		}
	}	
	return $datas;
}

// ============================= CETAK ==================================== //

function print_plain($id){
	$fields = my_get_data_by_id('spd','spd_id',$id);		
	if($fields['tipe_karyawan']=='Non Karyawan'){
		$karyawans = my_get_data_by_id('spd_non_karyawan','non_karyawan_id',$fields['karyawan_id'] );
		$golongan_jabatan_id = isset( $karyawans['golongan_jabatan_id'] ) ? $karyawans['golongan_jabatan_id'] :  0; 
		$golongans = my_get_data_by_id('golongan_jabatan','golongan_jabatan_id',$golongan_jabatan_id );
		$jenis_non_karyawan_id = isset( $karyawans['jenis_non_karyawan_id'] ) ? $karyawans['jenis_non_karyawan_id'] :  0; 
		$jenis_karyawans = my_get_data_by_id('spd_jenis_non_karyawan','jenis_non_karyawan_id',$jenis_non_karyawan_id );
		$nama=$karyawans['nama'];
		$nik='';
		$jabatan=$jenis_karyawans['label_jenis_non_karyawan'];
		$golongan=$golongans['kode_golongan_jabatan'];
		$unit_kerja=' ';
	}else{
		$karyawans = loaddata_karyawan($fields['karyawan_id']) ;
		$nama=$karyawans['nama_gelar'];
		$nik=$karyawans['nik'];
		$jabatans=loaddata_jabatan($karyawans['jabatan_id']);
		$jabatan=$jabatans['nama_jabatan'];
		$golongan=$karyawans['golongan_jabatan'];
		$unit_kerja=$jabatans['nomor_posisi'];
	}	
	$atemps = list_kalender($fields['tanggal_mulai'] , $fields['tanggal_selesai']);
	$jumlah_hari = count($atemps);		
	if( trim($fields['keperluan'])==''){
		$keperluans = my_get_data_by_id('spd_keperluan','keperluan_id',isset($fields['keperluan_id']) ? $fields['keperluan_id'] : 0);
		$keperluan=$keperluans['label_keperluan'] ;
	}else{
		$keperluan=$fields['keperluan'] ;
	}	
	$kendaraans = my_get_data_by_id('spd_kendaraan','kendaraan_id',isset($fields['kendaraan_id']) ? $fields['kendaraan_id'] : 0);
	$kendaraan=$kendaraans['label_kendaraan'] ;
	$view = '<script>window.print();</script>';
	$view .= '<table style="width: 100%;"><tr><td>';
	$view .='<table style="width:100%;border:1px solid;font-size:12px;">';
	$view .='<tr>
			<td valign="center" rowspan="3"><img src="logokm.png"></td>
			<td style="width= 50%;font-size:16px;vertical-text-align:middle;text-align:center"><b>PT KRAKATAU MEDIKA</b></td>
			</tr>';
	$view .='<tr>
		<td align="center">SURAT TUGAS / PERJALANAN DINAS</td>
		</tr>';	
	$view .='<tr>
			<td align="center">No.'.$fields['nomor'].'</td>
		</tr></table>' ;
	$view .='<table style="width:100%;border:1px solid;font-size:12px;">';
	$view .='<tr>
	<td style="width:10%">Nama</td>
	<td style="width:2%">:</td>
	<td style="width:48%">'.$nama.'</td>
	<td style="width:10%">NIK</td>
	<td style="width:2%">:</td>
	<td style="width:48%">'.$nik.'</td>
	</tr>';
	$view .='<tr>
	<td style="width:10%">Jabatan</td>
	<td style="width:2%">:</td>
	<td colspan="4">'.$jabatan.'</td>
	</tr>';
	$view .='<tr>
	<td style="width:10%">Pangkat</td>
	<td style="width:2%">:</td>
	<td colspan="4">'.$golongan.'</td>
	</tr>';
	$view .='<tr>
	<td style="width:10%">Unit&nbsp;Kerja</td>
	<td style="width:2%">:</td>
	<td colspan="4">'.$unit_kerja.'</td>
	</tr>';	
	$view .='<tr>
	<td style="width:10%">Tujuan</td>
	<td style="width:2%">:</td>
	<td colspan="4">'.$fields['kota'].'</td>
	</tr>';		
	$view .='<tr>
	<td style="width:10%">Tanggal</td>
	<td style="width:2%">:</td>
	<td colspan="3">'.date('d-m-Y',strtotime($fields['tanggal_mulai'])).' s/d '.date('d-m-Y',strtotime($fields['tanggal_mulai'])).'</td>
	<td align="right" >( '.$jumlah_hari.' ) Hari</td>
	</tr>';		
	$view .='<tr>
	<td style="width:10%">Keperluan</td>
	<td style="width:2%">:</td>
	<td colspan="4">'.$keperluan.'</td>
	</tr>';	
	$view .='<tr>
	<td colspan="6">&nbsp;</td>
	</tr>';		
	$view .='<tr>
	<td style="width:10%">Kendaraan</td>
	<td style="width:2%">:</td>
	<td colspan="4">'.$kendaraan.' : No. Pol : '.$fields['nomor_polisi'].'</td>
	</tr>';			
	$view .='<tr>
	<td colspan="6">Diperpanjang s/d tanggal</td>
	</tr>';			
	$view .='<tr>
	<td colspan="6">Karena</td>
	</tr>';			
	$view .='<tr>
	<td colspan="6">&nbsp;</td>
	</tr>';		
	$view .='<tr>
	<td colspan="6">&nbsp;</td>
	</tr>';		
	$view .='</table>';
	$view .= '<table width="100%" cellpading="0" cellspacing="0" style="border:1px solid;">';
	$view .='<tr>
		<td style="border-right:1px solid;text-align:center; height:24px;">
			Yang Menugaskan <br><br><br><br> ___________________ <br><br>
			
		</td>
		<td style="text-align:center; height:24px;">
			Dihitung oleh,<br>
			Pejabat Personalia<br><br><br> ___________________ <br><br>
		</td>
		</tr>';
	$view .='<tr>
		<td colspan="2" style="text-align:center;border-top:1px solid;">Diisi Pejabat di temp Tujuan
		</td>
		</tr>';		
	$view .='</table>';
	$view .= '<table width="100%" cellpading="0" cellspacing="0" style="border:1px solid;">';
	$view .='<tr style="vertical-align:top;text-align:center;height:100px;">
		<td style="border-right:1px solid;">Tiba Tgl</td>
		<td style="border-right:1px solid;">Selesai Tgl	</td>
		<td ">Tanda Tangan/Stempel</td>
		</tr>';	
	$view .='</table>';
	$view .='</td><td width="50% " style="vertical-align:bottom;text-align:left;">';
	$query = "SELECT * FROM spd_detail  a
		INNER JOIN spd_biaya b ON a.biaya_id = b.biaya_id
		WHERE a.spd_id ={$id} and a.total<>0 
		ORDER BY b.kode_biaya";
	$result = my_query($query);
	$i=$total=0;
			$view .='<b> PERINCIAN BIAYA </b><br> '; 
	while( $ey = my_fetch_array($result) ){
		$i++; 
		$total+=$ey['total'] ;
		$view .='<br> '
			.str_pad($i,3,' ', STR_PAD_LEFT).'. '
			.str_pad($ey['label_biaya'],25,'.', STR_PAD_RIGHT)
			.' Rp. '.str_pad(rp_format($ey['total']),10,' ', STR_PAD_LEFT)
			.'<br> '; 	
	}
	$view .='<br> '
		.str_pad('Total',28,'.', STR_PAD_BOTH)
		.' Rp. '.str_pad(rp_format($total),10,' ', STR_PAD_LEFT)
		.'<br> <br><br><br>'; 		
	/*
			.' Rp. '.str_pad(rp_format($ey['tarif']),10,' ', STR_PAD_LEFT).' X '
			.str_pad($ey['jumlah'].' hari',10,' ', STR_PAD_LEFT)
	*/
	$view .= '<table width="100%" cellpading="0" cellspacing="0" style="border:1px solid;">';
	$view .='<tr>
		<td style="border-right:1px solid;text-align:center;">
			Keuangan <br><br><br><br>
			
		</td>
		<td style="text-align:center;">
			Yang Menerima <br><br><br><br>
		</td>
		</tr>';
	
	$view .='</table>';	
	$view .='</td></tr></table>';
	return $view;
}